package com.start_eternity.oasissafewalk.controller;

import com.start_eternity.oasissafewalk.pojo.PageBean;
import com.start_eternity.oasissafewalk.comment.Result;
import com.start_eternity.oasissafewalk.pojo.Route;
import com.start_eternity.oasissafewalk.pojo.RouteStation;
import com.start_eternity.oasissafewalk.service.RouteService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;

@Slf4j
@RestController
@RequestMapping("/routes")
public class RouteController {

    @Autowired
    private RouteService routeService;

    //查询线路
    @GetMapping
    public Result page(@RequestParam(defaultValue = "1") Integer page,
                       @RequestParam(defaultValue = "10")Integer pageSize,
                       String routeName, Short routeStatus,
                       @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate begin,
                       @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate end){
        log.info("分页查询,参数:{},{},{},{},{},{}",page,pageSize,routeName,routeStatus,begin,end);

        //调用service方法进行分页查询
        PageBean pageBean = routeService.page(page,pageSize,routeName,routeStatus,begin,end);
        return Result.success(pageBean);
    }

    //新增线路
    @PostMapping
    public Result add(@RequestBody Route route){
        log.info("添加线路");

        routeService.add(route);

        return Result.success();
    }

    //删除线路
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id){
        log.info("根据id删除线路:{}",id);

        routeService.delete(id);

        return Result.success();
    }

    //修改线路
    //获取线路id
    @GetMapping("/{id}")
    public Result getById(@PathVariable Integer id){
        log.info("根据线路id：{},获取该对象",id);

        Route route = routeService.getById(id);

        return Result.success(route);
    }

    //修改
    @PutMapping
    public Result update(@RequestBody Route route){
        log.info("修改线路:{}",route);

        routeService.update(route);

        return Result.success();
    }

    //查询线路中的所有站点(多变查询)
    @GetMapping("/{routeId}/allStations")
    public Result getRouteWithStations(@PathVariable Integer routeId) {
        log.info("获取该线路id：{}中的所有站点",routeId);
        Route route = routeService.getRouteWithStations(routeId);

        return Result.success(route);
    }

    //新增线路站点
    @PostMapping("/routeStation")
    public Result addRouteStation(@RequestBody RouteStation routeStation){

        log.info("在线路里添加站点");

        routeService.addRouteStation(routeStation);

        return Result.success();
    }

    //删除线路站点
    @DeleteMapping("/routeStation/{routeStationId}")
    public Result deleteRouteStation(@PathVariable Integer routeStationId){
        log.info("根据id删除线路:{}",routeStationId);

        routeService.deleteRouteStation(routeStationId);

        return Result.success();
    }

    //修改线路站点
    //获取线路站点id
    @GetMapping("/routeStation/{routeStationId}")
    public Result getOneByRouteStationId(@PathVariable Integer routeStationId){
        log.info("根据线路站点id：{},获取该对象",routeStationId);

        RouteStation routeStation = routeService.getOneByRouteStationId(routeStationId);

        return Result.success(routeStation);
    }

    //修改
    @PutMapping("/routeStation")
    public Result updateRouteStation(@RequestBody RouteStation routeStation){
        log.info("修改线路站点:{}",routeStation);

        routeService.updateRouteStation(routeStation);

        return Result.success();
    }

}
